package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.CityMapper;
import com.code.dao.VillageMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.City;
import com.code.pojo.Village;
import com.code.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class VillageServiceImpl implements VillageService {

    @Autowired
    VillageMapper villageMapper;
    @Autowired
    CityMapper productMapper;


    @Override
    public PageResult<Village> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Village> villageList = villageMapper.findLike(queryPageBean);
        PageInfo<Village> pageInfo = new PageInfo<>(villageList);
        PageResult<Village> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Village> findPage(Village village) {
//     PageHelper.startPage(village.getPage(),village.getSize());
//        List<Village> villageList = villageMapper.findLike(village);
//        PageInfo<Village> pageInfo = new PageInfo<>(villageList);
//        PageResult<Village> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Village findById(Integer id) {
        return villageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Village village) {
        villageMapper.insert(village);
    }

    @Override
    public void update(Village village) {
        villageMapper.updateByPrimaryKeySelective(village);
    }

    @Override
    public void delete(Integer id) {
        villageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<City> findCity() {
        return productMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return villageMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return villageMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Village> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Village> villageList = villageMapper.findPageNo(queryPageBean);
        PageInfo<Village> pageInfo = new PageInfo<>(villageList);
        PageResult<Village> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Village village = villageMapper.selectByPrimaryKey(id);
        village.setState(1);
        villageMapper.updateByPrimaryKeySelective(village);
    }
}
